//
//  UIColor+GAUtility.m
//
//  Created by GikkiAres on 2016/12/1.
//

#import "UIColor+JsUtility.h"

@implementation UIColor (JsUtility)
+(instancetype)randomColor {
    CGFloat red = (arc4random()%101)/100.0;
    CGFloat green = (arc4random()%101)/100.0;
    CGFloat blue = (arc4random()%101)/100.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}
+ (UIColor *)colorWithHex:(long)hexColor{
    return [UIColor colorWithHex:hexColor alpha:1];
}
/**
 alpha是透明度和opacity是相反的.
 */
+ (UIColor *)colorWithHex:(long)hexColor alpha:(float)alpha
{
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}


+(instancetype)grayColorWithWhite:(int)white alpha:(int)alpha {
	return [UIColor colorWithRed:white/255.0 green:white/255.0 blue:white/255.0 alpha:alpha/255.0];
}
+(instancetype)grayColorWithWhite:(int)white {
	return [UIColor colorWithRed:white/255.0 green:white/255.0 blue:white/255.0 alpha:1];
}
	//10进制,rgb∈[0,255],a∈[0,1]
+(instancetype)colorMax255WithR:(int)r g:(int)g b:(int)b a:(int)a {
     return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a/255.0];
}

+(instancetype)colorWithRGBADecimalString:(NSString *)str {
    NSArray *arrColorComponent = [str componentsSeparatedByString:@","];
    if (arrColorComponent.count==3) {
        return [UIColor colorMax255WithR:[arrColorComponent[0] doubleValue] g:[arrColorComponent[1] doubleValue]  b:[arrColorComponent[2] doubleValue]  a:1];
    }
    else if(arrColorComponent.count==4)  {
        return [UIColor colorMax255WithR:[arrColorComponent[0] doubleValue] g:[arrColorComponent[1] doubleValue]  b:[arrColorComponent[2] doubleValue]  a:[arrColorComponent[3] doubleValue]];
    }
    else {
        return [UIColor whiteColor];
    }
}



-(NSString *)rgbaHexString {
	const CGFloat *components = CGColorGetComponents(self.CGColor);
	int i_dr = components[0] * 255;
	int i_dg = components[1] * 255;
	int i_db = components[2] * 255;
	int i_da = components[3] * 255;
	NSString * string = [NSString stringWithFormat:@"%02x%02x%02x%02x",i_dr,i_dg,i_db,i_da];
	return string;
}

#pragma mark 2 String -> Color
#pragma mark 2.1 16进制RGB字符串转颜色
+(instancetype)colorWithRGBHexString:(NSString *)str {
	if(str.length != 6) {
		return nil;
	}
	NSString * str2 = [str stringByAppendingString:@"ff"];
	return [self colorWithRGBAHexString:str2];
}

#pragma mark 2.2 16进制RGBA字符串转颜色
+ (UIColor *)colorWithRGBAHexString:(NSString *)str {
	if(str.length !=8) {
		return [UIColor whiteColor];
	}
	NSString * r = [str substringWithRange:NSMakeRange(0, 2)];
	float r1 = strtoul(r.UTF8String, 0, 16);
	
	NSString * g = [str substringWithRange:NSMakeRange(2, 2)];
	float g1 = strtoul(g.UTF8String, 0, 16);
	
	NSString * b = [str substringWithRange:NSMakeRange(4, 2)];
	float b1 = strtoul(b.UTF8String, 0, 16);
	
	NSString * a = [str substringWithRange:NSMakeRange(6, 2)];
	float a1 = strtoul(a.UTF8String, 0, 16);
	
	return [UIColor colorWithRed:r1/255 green:g1/255 blue:b1/255 alpha:a1/255];
}


@end
